﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using LifeTree.Core;
using LifeTree.Mvc.ViewModels;
using AutoMapper;
using LifeTree.Core.Utils;
using LifeTree.DomainModel.Repositories;
using LifeTree.DomainModel;
using LifeTree.Core.DataAccess;
using LifeTree.DomainModel.Entities;
using LifeTree.Mvc.ActionResults;
using LifeTree.Mvc.Helpers;
using LifeTree.Mvc.Extensions;

namespace LifeTree.Mvc.Controllers
{
    public class TaskController : BaseController
    {
        IViewHelper viewHelper = null;

        public TaskController()
        {
            viewHelper = IoC.Resolve<IViewHelper>();
        }

        [HttpPost]
        public ActionResult Create(TaskViewModel model, string workBookName)
        {
            Workbook workBook = unitOfWork.WorkbookRepository.GetByName(workBookName);
            if (workBook == null)
                return new NotFoundResult() { Message = "Workbook is not found." };

            Task task = entityFactory.CreateTask(model.Text);
            if (!TryValidateModel(task))
                return new JsonResult() { Data = ModelState.GetErrors() };

            workBook.Tasks.Add(task);
            unitOfWork.SaveChanges();

            var viewModel = Mapper.Map<Task, TaskViewModel>(task);
            return Json(new { html = viewHelper.RenderPartialView(ControllerContext, "TaskItem", viewModel) });
        }

        [HttpGet]
        public ActionResult New()
        {
            return View("Edit", new TaskViewModel());
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            var task = unitOfWork.TaskRepository.GetById(id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            unitOfWork.TaskRepository.Remove(task);
            unitOfWork.SaveChanges();

            return Json(new { status = "ok" });
        }

        [HttpPost]
        public ActionResult Update(TaskViewModel model)
        {
            var task = unitOfWork.TaskRepository.GetById(model.Id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            task.Text = model.Text;
            task.ModifiedDate = DateTime.Now;

            if (!TryValidateModel(task))
                return new JsonResult() { Data = ModelState.Values };

            unitOfWork.SaveChanges();

            return Json(new { text = model.Text });
        }

        [HttpGet]
        public ActionResult Item(string id)
        {
            var task = unitOfWork.TaskRepository.GetById(id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            var viewModel = Mapper.Map<Task, TaskViewModel>(task);
            return Json(viewModel, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Finish(string id)
        {
            var task = unitOfWork.TaskRepository.GetById(id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            task.Status = TaskStatuses.Finished;
            task.ModifiedDate = DateTime.Now;
            task.FinishedDate = DateTime.Now;

            unitOfWork.SaveChanges();
            return Json(new { status = "ok" });
        }

        [HttpPost]
        public ActionResult Activate(string id, string workBookName)
        {
            var task = unitOfWork.TaskRepository.GetById(id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            var workBook = unitOfWork.WorkbookRepository.GetByName(workBookName);

            var activeTask = workBook.GetActiveTask();
            if (activeTask != null)
                return new JsonErrorResult(100, "There is another activated task. Reload page please.");

            task.Status = TaskStatuses.Active;
            task.ModifiedDate = DateTime.Now;
            task.FinishedDate = DateTime.Now;

            unitOfWork.SaveChanges();
            return Json(new { status = "ok" });
        }

        [HttpPost]
        public ActionResult Copy(string id, string workBookName)
        {
            var task = unitOfWork.TaskRepository.GetById(id);
            if (task == null)
                return new NotFoundResult() { Message = "Task is not found." };

            var newTask = entityFactory.CreateTask(task.Text);
            var workBook = unitOfWork.WorkbookRepository.GetByName(workBookName);
            workBook.Tasks.Add(newTask);
            unitOfWork.SaveChanges();
            return Json(new { id = newTask.Id });
        }
    }
}
